/*
    This file is part of the substandard lisp implementation of Standard Lisp
    Copyright (C) 2011 Nicky Nickell

    substandard lisp is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

std_string_trim = (a, env) ->
  bag = a[0]
  str = a[1].value

  if bag.type is TYPE_PAIR
    bag = list_to_array bag
  else if bag.type is TYPE_STRING
    bag = (bag.value.charCodeAt(i) for i in [0...bag.value.length])

  while str.charCodeAt(0) in bag
    str = str.slice(1)

  while str.charCodeAt(str.length - 1) in bag
    str = str.substr(0, str.length - 1)

  return new lisp_string str

/*---
name: string-trim
arity: 2
rest: false
ftype: FTYPE_EXPR

argument-types:
  - TYPE_PAIR | TYPE_STRING
  - TYPE_STRING

doc: |
  explaining what string-trim is / how it is used!

tests: |
  (string-trim "abc" "abcfooabc") -> "foo"
...*/
